<template>
  <div class="iii">
    <!-- 轮播图数据 -->
    <van-swipe class="my-swipe" :autoplay="3000" indicator-color="yellow">
      <van-swipe-item v-for="item in swiperarr" :key="item.banner_id">
        <img :src="item.banner_url" alt="">
      </van-swipe-item>
    </van-swipe>
    <!-- 宫格 -->

    <van-grid :column-num="5">
      <van-grid-item v-for="value in pageList" :key="value.page_id" :icon="value.page_url" :text="value.page_title"
        @click="fenlei(value.page_id)" />
    </van-grid>
    <div class="hot">
      <p class="hot1">{{ HotDatatitle }}</p>
      <ul id="hot2">
        <li v-for="item in productHotList">
          <img :src="item.product_top_img_1" alt="">
          <p>{{ item.product_name }}</p>
          <p class="hot4">
            <span>{{ item.product_original_price }}￥</span>
            <span style="text-decoration:line-through; color: #ccc;"> {{ item.product_present_price }}￥</span>
          </p>
          <p class="hot5">
            <van-button type="warning" size="small"
              @click="addcart(item.product_name, item.product_top_img_1, item.product_original_price, item.product_id)"><van-icon
                name="shopping-cart-o" /></van-button>
            <van-button type="danger" size="small" @click="goxiang(item.product_id)">查看详情</van-button>
          </p>
        </li>
      </ul>
    </div>
    <h2>
      <span>1F</span>
      休闲食品
    </h2>
    <ul class="onef">
      <li v-for="item in ProductHotList" @click="goxiang(item.product_code
      )">
        <img :src="item.titleImg" alt="">
      </li>
    </ul>
    <h2>
      <span>2F</span>
      新鲜水果
    </h2>
    <ul class="onef">
      <li v-for="item in ProductHotList2f" @click="goxiang(item.product_code
      )">
        <img :src="item.titleImg" alt="">
      </li>
    </ul>
    <h2>
      <span>3F</span>
      营养奶品
    </h2>
    <ul class="onef">
      <li v-for="item in ProductHotList3f" @click="goxiang(item.product_code
      )">
        <img :src="item.titleImg" alt="">
      </li>
    </ul>
    <p class="btm"></p>
  </div>
</template>

<script setup lang='ts'>

import { onMounted, ref } from 'vue';
import router from '@/router';
import { homedata, addShoppingCart } from '../api/http'
import { Toast } from 'vant';


const swiperarr = ref()
const pageList = ref()
const HotDatatitle = ref()
const productHotList = ref()
// 1f 2f 3f
const ProductHotList = ref()
const ProductHotList2f = ref()
const ProductHotList3f = ref()

// 热销的添加购物车
const addcart = (name: any, img: any, price: any, id: any) => {
  addShoppingCart({
    shop_userCode: localStorage.getItem('user_code'),
    shop_proTitle: name,
    shop_proPrice: price,
    shop_proImg: img,
    shop_proCount: 1,
    shop_proId: id
  }).then((res: any) => {
    console.log(res);
    if (res.responseCode == '1000') {
      alert(res.responseMsg)
    }
  })
}
const goxiang = (id: any) => {
  router.push({ path: '/xq', query: { type: id } })
}

const fenlei = (item: any) => {
  router.push({ path: '/fenlei', query: { type: item } })
}
onMounted(() => {
  homedata({}).then((res: any) => {
    console.log('首业数据', res);
    swiperarr.value = res.bannerList
    pageList.value = res.pageList
    HotDatatitle.value = res.HotData.title
    productHotList.value = res.HotData.productHotList
    ProductHotList.value = res.ProductHotList.slice(0, 5)
    ProductHotList2f.value = res.ProductHotList.slice(5, 10)
    ProductHotList3f.value = res.ProductHotList.slice(10, 15)


  })


})
</script>
<style  lang="scss">
#app {
  margin: 0;
  padding: 0;

  .btm {
    width: 100%;
    height: 3rem;
  }

  .onef {
    width: 100%;
    height: 15rem;

    li:first-child {
      height: 10rem;
    }

    li {
      width: 50%;
      height: 5rem;
      float: left;

      img {
        width: 100%;
        height: 100%;
      }
    }
  }

  h2 {
    text-align: center;
    font-weight: 400;
    color: red;
    // display: flex;
    // justify-content: space-between;

    span {
      background-color: red;
      color: #fff;
      border-radius: 50%;
      // font-size: 1rem;
    }
  }

  .hot {
    width: 100%;
    display: flex;
    flex-direction: column;
    background: #fff;
    margin-top: 1rem;

    .hot1 {
      margin-left: 1rem;
    }

    #hot2 {
      width: 100%;
      height: 11rem;
      background-color: antiquewhite;
      display: flex;
      flex-wrap: nowrap;
      flex-shrink: 0;
      overflow: scroll;
      overflow-y: hidden;

      li {
        display: flex;
        flex-direction: column;
        align-items: center;
        flex: none;
        float: left;
        width: 33.3%;
        height: 10rem;
        border-top: 0.05rem solid #cccc;
        border-right: 0.05rem solid #cccc;


        img {
          width: 100%;
          height: 60%;
        }

        p {
          width: 71%;
          font-size: 0.8rem;
          white-space: nowrap;
          overflow: hidden;
          text-align: center;
          text-overflow: ellipsis;
        }

        .hot4 {
          width: 50%;
        }

        .hot5 {
          width: 80%;
          display: flex;
        }
      }
    }
  }

}

.iii {
  background-color: rgb(219, 216, 216);
  height: 50rem;

  .van-grid {
    width: 96%;
    border-radius: 0.2rem;
    margin: 0.5rem auto 0;
  }

  .my-swipe .van-swipe-item {
    height: 15rem;

    img {
      width: 100%;
      height: 100%;
    }
  }
}
</style>
